﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Publicuse.Entity.AttributeUtil.Validate
{
    /// <summary>
    /// 数字校验规则验证
    /// </summary>
    public class NumberrangeAttribute : BaseValidateAttribute
    {
        private double min = 0;
        private double max = 0;
        private int len = 0;
        private string remark = null;

        public string GetRemark { get { return this.remark; } }

        /// <summary>
        /// 设置规则
        /// </summary>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <param name="len">小数位数</param>
        /// <param name="remark">描述</param>
        public NumberrangeAttribute(double min, double max,int len,string remark)
        {
            this.max = max;
            this.min = min;
            this.len = len;
            this.remark = remark;
        }

        public override bool Validate(object oValue)
        {
            if (oValue != null && !string.IsNullOrEmpty(oValue.ToString()))
            {
                var inputvalue = oValue.ToString();
                if (inputvalue.Split('.').Length == 1)
                {
                    return false;
                }
                var input = Convert.ToDouble(inputvalue);
                if (input >= min && input <= max && inputvalue.Split('.')[1].Length <= 8)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
    }
}
